package com.jp.knowledge.view;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.my.util.MyUtil;

/**
 * Created by 528787067@qq.com on 2017/3/2.
 */

public class PaySelectView extends FrameLayout implements View.OnClickListener {

    public PaySelectView(Context context) {
        super(context);
        init();
    }

    public PaySelectView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PaySelectView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    protected ViewGroup preSelectIcon;
    protected LinearLayout moneyBtn1;
    protected LinearLayout moneyBtn5;
    protected LinearLayout moneyBtn8;
    protected LinearLayout moneyBtn10;
    protected LinearLayout moneyBtn50;
    protected LinearLayout moneyBtn99;
    protected LinearLayout moneyBtnOther;
    protected EditText moneyInput;
    protected void init(){
        View contentView = inflate(getContext(), R.layout.activity_pay_select_view, null);
        moneyBtn1 = (LinearLayout) contentView.findViewById(R.id.money_1);
        moneyBtn5 = (LinearLayout) contentView.findViewById(R.id.money_5);
        moneyBtn8 = (LinearLayout) contentView.findViewById(R.id.money_8);
        moneyBtn10 = (LinearLayout) contentView.findViewById(R.id.money_10);
        moneyBtn50 = (LinearLayout) contentView.findViewById(R.id.money_50);
        moneyBtn99 = (LinearLayout) contentView.findViewById(R.id.money_99);
        moneyBtnOther = (LinearLayout) contentView.findViewById(R.id.money_other);
        moneyInput = (EditText) contentView.findViewById(R.id.money_input);
        moneyInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                String inputMoney = moneyInput.getText().toString();
                int pointIndex = inputMoney.indexOf('.');
                if (pointIndex == 0)
                    inputMoney = 0 + inputMoney;
                if(pointIndex > 0 && inputMoney.length() > pointIndex+3)
                    inputMoney = inputMoney.substring(0, pointIndex+3);
                if (!moneyInput.getText().toString().equals(inputMoney)) {
                    moneyInput.setText(inputMoney);
                    moneyInput.setSelection(inputMoney.length());
                }

                if(onSelectedNumChangedListener != null)
                    onSelectedNumChangedListener.onSelectedNumChanged(getSelectMoney());
            }
        });

        moneyBtn1.setTag(100);
        moneyBtn5.setTag(500);
        moneyBtn8.setTag(800);
        moneyBtn10.setTag(1000);
        moneyBtn50.setTag(5000);
        moneyBtn99.setTag(9900);
        moneyBtnOther.setTag(0);
        moneyBtn1.setOnClickListener(this);
        moneyBtn5.setOnClickListener(this);
        moneyBtn8.setOnClickListener(this);
        moneyBtn10.setOnClickListener(this);
        moneyBtn50.setOnClickListener(this);
        moneyBtn99.setOnClickListener(this);
        moneyBtnOther.setOnClickListener(this);
        moneyInput.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP)
                    onClick(moneyBtnOther);
                return false;
            }
        });

        addView(contentView);
        onClick(moneyBtn1);
    }

    @Override
    public void onClick(View view) {
        if(preSelectIcon == view)
            return;
        if(preSelectIcon != null) {
            preSelectIcon.setBackground(getResources().getDrawable(R.drawable.pay_icon_unselect));
            for (int i = 0; i < preSelectIcon.getChildCount(); i++)
                ((TextView) preSelectIcon.getChildAt(i)).setTextColor(Color.parseColor("#F85959"));
        }
        view.setBackground(getResources().getDrawable(R.drawable.pay_icon_selected));
        for(int i = 0; i < ((ViewGroup)view).getChildCount(); i++)
            ((TextView)((ViewGroup)view).getChildAt(i)).setTextColor(getResources().getColor(R.color.white));
        if(view == moneyBtnOther) {
            moneyInput.setTextColor(Color.parseColor("#F85959"));
            MyUtil.startKeyword(getContext(), moneyInput);
        } else if(preSelectIcon == moneyBtnOther){
            moneyInput.setTextColor(getResources().getColor(R.color.font_gray_light));
            MyUtil.closeKeyword(getContext(), moneyInput);
        }
        preSelectIcon = (ViewGroup) view;

        if(onSelectedNumChangedListener != null)
            onSelectedNumChangedListener.onSelectedNumChanged(getSelectMoney());
    }

    public void hideKeyboard(){
        MyUtil.closeKeyword(getContext(), moneyInput);
    }

    /* 获取支付金额，返回以“分”为单位 */
    public int getSelectMoney(){
        if(preSelectIcon == null)
            return -1;
        if(preSelectIcon == moneyBtnOther && moneyInput.getText().toString().trim().length() > 0)
            try {
                String money = moneyInput.getText().toString();
                int pointPosition = money.indexOf(".");
                if(pointPosition < 0)
                    money += ".00";
                if(money.length()-pointPosition == 1)
                    money += "00";
                else if(money.length()-pointPosition == 2)
                    money += "0";
                else
                    money = money.substring(0, pointPosition+3);
                money = money.replace(".", "");
                return Integer.parseInt(money);
            } catch (Exception e){
                return -1;
            }
        else if(preSelectIcon == moneyBtnOther)
            return -1;
        return (int)preSelectIcon.getTag();
    }

    private OnSelectedNumChangedListener onSelectedNumChangedListener;
    public void setOnSelectedNumChangedListener(OnSelectedNumChangedListener onSelectedNumChangedListener){
        this.onSelectedNumChangedListener = onSelectedNumChangedListener;
    }
    public interface OnSelectedNumChangedListener{
        void onSelectedNumChanged(int num);
    }
}
